<?php
namespace Admin\Model;

use Admin\Model\BaseModel;

class AskCategoryModel extends BaseModel{
    protected $_validate = array(
        //array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),
    	array('ask_category_name', 'require', '请输入文章分类', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    	array('ask_category_name', 'checkName', '文章分类已经存在', self::MUST_VALIDATE, 'callback', self::MODEL_BOTH),
    	array('ask_category_name', '1,20', '文章分类不能超过20个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
    	array('sort', 'number', '排序必须是数字', self::MUST_VALIDATE, '', self::MODEL_BOTH),
    );
    
    protected $_auto = array(
        //array(完成字段1,完成规则,[完成条件,附加规则]),
    	array('create_time', NOW_TIME, self::MODEL_INSERT),
    	array('update_time', NOW_TIME, self::MODEL_UPDATE),
    );
    
    /**
     * @todo 检测名称是否存在
     * @param  string   $ask_category_name 文章分类名称
     * @return boolean     ture - 未重名，false - 已重名
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    protected function checkName($ask_category_name){
    	$ask_category_id = (int)I('ask_category_id');
    	$ask_category_pid = (int)I('ask_category_pid');
    	//有上级的情形
    	if($ask_category_pid > 0){
    		$map['ask_category_pid'] = $ask_category_pid;
    	}else{
    		$map['ask_category_pid'] = 0;
    	}
    	if($ask_category_id >　0){
    		$map['ask_category_id'] = array('neq',$ask_category_id);
    	}
    	$map['ask_category_name'] = $ask_category_name;
    	$map['status'] = array('gt',-1);
    	if ($this->where($map)->field('ask_category_id')->find()){
    		return false;
    	}
    	return true;
    }
    
    /**
     * @todo 商品一级品类详情
     * @param int $ask_category_id 商品一级品类id
     * @param string $field 查询字段
     * @return false|array
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function AskCategoryShow($ask_category_id = 0,$field = true){
    	//获取数据
    	$ask_category_id = (int)$ask_category_id;
    	if($ask_category_id == 0){
    		$this->error = '请正确操作';
    		return false;
    	}
    	$map = array();//初始化查询条件
    	//查询条件
    	$map['ask_category_id'] = $ask_category_id;
    	//返回值
    	return $this->where($map)->field($field)->find();
    }
    
    /**
     * @todo 判断一级品类下是否有二级品类 删除验证
     * @param string $ask_category_id
     * @return boolean false 没有下级品类，true 存在下级品类
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function judgeAskCategory($ask_category_id){
    	if(empty($ask_category_id)){
    		$this->error = '参数丢失，请重试';
    		return false;
    	}
    	$map['status'] = array('gt',-1);
    	$map['ask_category_pid'] = array('in',$ask_category_id);
    	$data = $this->where($map)->field('ask_category_id')->find();
    	if($data){
    		return false;
    	}else{
    		return true;
    	}
    }
    
    /**
     * @todo 判断分类是否绑定文章
     * @param string $class_ids
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function judgeArticleCategory($id){
    	if(empty($id)){
    		$this->error = '参数丢失，请重试';
    		return false;
    	}
    	$map['ask_category_id'] = array('in',$id);
    	$map['status'] = array('gt',-1);
    	$article_id = M('AskArticle')->where($map)->field('ask_article_id')->find();
    	if(!empty($article_id)){
    		return false;
    	}
    	return true;
    }
    
    /**
     * @todo 判断分类是否绑定标签
     * @param string $class_ids
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function judgeTagsCategory($id){
    	if(empty($id)){
    		$this->error = '参数丢失，请重试';
    		return false;
    	}
    	$map['ask_category_id'] = array('in',$id);
    	$map['status'] = array('gt',-1);
    	$tags_id = M('AskCategoryTags')->where($map)->field('ask_tags_id')->find();
    	if(!empty($tags_id)){
    		return false;
    	}
    	return true;
    }
    
    /**
     * @todo 取出一级文章分类
     * @return array
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function firstCategoryShow(){
    	//获取数据
    	$map['ask_category_pid'] = 0;
    	$map['status'] = 1;
    	$first_ask_gategory = $this->where($map)->field('ask_category_id,ask_category_name')->select();
    	if(empty($first_ask_gategory)){
    		return array();  //没有数据返回空数组
    	}
    	return $first_ask_gategory;
    }
    
    /**
     * @todo 返回主键
     * @see \Think\Model::getPk()
     */
    public function getPk(){
        return 'ask_category_id';
    }
    
    /**
     * @todo 返回最后一次错误信息
     * @return string
     * @author Cruise <jiangyu@hrbjjhh.com>
     */
    public function getError(){
    	return $this->error;
    }
    
}